# Show your ip, compares it to what it may have used to be.
# You must have root level permissions to run the script (it accesses ifconfig)


#!/bin/sh

addy=your@email.com
tmp_file=/tmp/.myip

whoami=`whoami`
if [[ ${whoami} != 'root' ]]
then
  echo "You must be root to run this program"
  exit 0
fi
## have to get old ip 1st
if [[ -f ${tmp_file} ]]
then
  myip=`cat ${tmp_file}`
  oldip=`cat ${tmp_file}`
else
  touch ${tmp_file}
  newrun=1
fi

## Dialup users:
/sbin/ifconfig | \
sed -n -e '/ppp0\|ppp1\|ppp2\|ppp3/,/$$/!d' -e \
's/.*addr:\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' > ${tmp_file}

## Lan users:
/sbin/ifconfig | \
sed -n -e '/eth0\|eth1/,/$$/!d' -e \
's/.*addr:\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' > ${tmp_file}

## Remove loopback:
cat ${tmp_file} | sed -e 's/127.0.0.1//p' > ${tmp_file}

myip=`cat ${tmp_file}`

test "$myip" = "$oldip"
#echo $?
#echo $myip
if [ $? -eq 1 ]; then
## Uncomment the following line if you wish to have the alert e-mailed to you.
#  mail $addy -s Alert: ip has changed -f Your ip has expired.  New ip is $myip
  if [ $newrun -eq 1 ]; then
    echo Setting up ip for the 1st time.
    echo New ip is $myip
  else
    echo Alert: ip has changed!
    echo New ip is $myip
  fi
  else echo ip remains $myip
fi
